<?php

function totalWinningsSort($a, $b) {
  return $b->total - $a->total;
}

function averageWinningsSort($a, $b) {
  return $b->average - $a->average;
}

function highWinningsSort($a, $b) {
  return $b->high - $a->high;
}

function lowWinningsSort($a, $b) {
  return $b->low - $a->low;
}

class Winnings {

  var $winnings;
  var $gates;

  function Winnings($season) {
    $this->winnings = array();
    $rs = executeQuery("select team_id, sum(winnings), floor(sum(winnings) / count(winnings)), max(winnings), min(winnings) from match_records mr, matches ma, seasons_teams st where st.fk_season_id=$season->id and ma.fk_season_id=$season->id and ma.match_id =mr.match_id and team_id=fk_team_id group by team_id");
    while($row = mysql_fetch_row($rs)) {
      array_push($this->winnings, new Winning($row[0], $row[1], $row[2], $row[3], $row[4]));
    }

    $this->gates = array();
    $rs = executeQuery("select team_id, floor(sum(gate) / 1000), floor(sum(gate) / (count(gate) * 1000)), floor(max(gate) / 1000), floor(min(gate) / 1000) from match_records mr, seasons_teams st, matches ma where ma.fk_season_id=$season->id and st.fk_season_id=$season->id and mr.team_id=st.fk_team_id and mr.match_id=ma.match_id group by team_id");
    while($row = mysql_fetch_row($rs)) {
      array_push($this->gates, new Winning($row[0], $row[1], $row[2], $row[3], $row[4]));
    }
  }
}
?>